package tedu.week03.io;



import org.junit.Test;

import java.io.*;

/**
 * <pre>
 *      InputStream/OutputStream测试.
 *
 * </pre>
 *
 * @author : resty
 * @date : 2019年08月11日    23时
 **/
public class InputDemo {
    /**
     * <b>
     *     在原有内容基础上添加内容并实现换行添加
     * </b>
     * <pre>
     *         OutputStream osm = new FileOutputStream(file,true);
     *         String str = "\r\nHello World!!!";
     *         osm.write(str.getBytes());
     *         osm.close();
     * </pre>
     * @param file 指定的文件
     * @throws IOException 抛出异常
     */
    public static void outputMth3(File file)throws IOException{
        OutputStream osm = new FileOutputStream(file,true);
        String str = "\r\nHello World!!!";
        osm.write(str.getBytes());
        osm.close();
    }

    /**write(byte bs[]).
     *  <pre>
     *      OutputStream osm = new FileOutputStream(file);
     *      String str = "Hello World!!!";
     *      osm.write(str.getBytes());
     *      osm.close();
     *  </pre>
     *  <b>
     *      写入一个数组
     *  </b>
     * @param file 传入的文件
     * @throws IOException 抛出异常
     */
    public static void outputMth2(File file) throws IOException{
        OutputStream osm = new FileOutputStream(file);
        String str = "Hello World!!!";
        osm.write(str.getBytes());
        osm.close();
    }

    /**write(byte b).
     *         <pre>
     *          OutputStream osm = new FileOutputStream(file);
     *          String str = "Hello World!!!";
     *          byte bs [] = str.getBytes();
     *          for (int i = 0; i &lt; bs.length; i++) {
     *             osm.write(bs[i]);
     *          }
     *          osm.close();
     *          </pre>
     *          <b>
     *              每次写入一个字节.
     *          </b>
     * @param file 传入的文件
     * @throws IOException 抛出异常
     */
    public static void outputMth1(File file) throws IOException {
        OutputStream osm = new FileOutputStream(file);
        String str = "Hello World!!!";
        byte bs [] = str.getBytes();
        for (int i = 0; i < bs.length; i++) {
            osm.write(bs[i]);
        }
        osm.close();
    }


    /**
     * read().
     *  <pre>
     *         InputStream ism = new FileInputStream(file);
     *         byte bs[] = new byte[1024];
     *         int len =0;
     *         int temp=0;
     *         while((temp=ism.read())!=-1){
     *             bs[len]= (byte) temp;
     *             len++;
     *         }
     *         String str = new String(bs);
     *         System.out.println(str);
     *         ism.close();
     *  </pre>
     *  <b>
     *      read()将文件中的每个字节都进行迭代.直到最后一个.返回值为-1.
     *  </b>
     *
     * @param file  指定的文件
     * @throws IOException 忽略异常
     */
    public static void inputMth3(File file) throws IOException{
        InputStream ism = new FileInputStream(file);
        byte bs[] = new byte[1024];
        int len =0;
        int temp=0;
        while((temp=ism.read())!=-1){
            bs[len]= (byte) temp;
            len++;
        }
        String str = new String(bs);
        System.out.println(str);
        ism.close();

    }





    /**
     *  read(byte bs[]) / file.length().
     *      <pre>
     *          InputStream ism = new FileInputStream(file);
     *          int len = (int) file.length();//文件的大小
     *          byte bs[] = new byte[len];
     *          ism.read(bs);
     *          String str = new String(bs);
     *          System.out.println(str);
     *          ism.close();
     *      </pre>
     *      <b>
     *          数组的长度可以由file.length()去确定.
     *              如果有一个文件过大的话,那么读取的时间就会特别的长.
     *      </b>
     * @param file 指定的文件
     * @throws IOException 忽略异常
     */
    public static void inputMth2(File file)throws  IOException{
        InputStream ism = new FileInputStream(file);
        int len = (int) file.length();//文件的大小
        byte bs[] = new byte[len];
        ism.read(bs);

        String str = new String(bs);
        System.out.println(str);
        ism.close();

    }

    /**
     * read(byte bs[]).
     *      <pre>
     *          InputStream ism = new FileInputStream(file);
     *          byte bs[] = new byte[1024];
     *          ism.read(bs);
     *          String str = new String(bs);
     *          System.out.println(str);
     *          ism.close();
     *      </pre>
     *      <p>
     *          因为一个数组的长度是固定的,那么就有可能出现两种情况:
     *              第一种,文件大,数组小.数组中存放的数据就不完全.
     *              第二种,文件小,数组大.数组中就会有一部分空位存在.读取就会有空位.
     *      </p>
     * @param file 指定的文件
     * @throws IOException 忽略异常
     */
    public static void inputMth1(File file) throws IOException{
        InputStream ism = new FileInputStream(file);
        byte bs[] = new byte[1024];
        ism.read(bs);
        String str = new String(bs);
        System.out.println(str);
        ism.close();
    }


    public static void main(String[] args) throws IOException {
        File file = new File("resty.primary/src/main/resources/input.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
/**
 * 输出流.
 */
        outputMth1(file);

/**
 * 输入流
 */
//        inputMth3(file);


    }
}
